<?php 
class ControllerCheckoutAddress extends Controller {
	var $error = array(); 
	
	function shipping() {
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		 
    	$language->load('controller/checkout_address.php');

    	$template->set('title', $language->get('heading_title')); 

		if (!$customer->isLogged()) {  
			$session->set('redirect', $url->ssl('checkout_shipping'));
      		
			$response->redirect($url->ssl('account_login'));
    	}
				  
    	if (($request->isPost()) && ($request->has('address_id', 'post'))) {
	  		$session->set('shipping_address_id', $request->gethtml('address_id', 'post'));

			$response->redirect($url->ssl('checkout_shipping'));
		}
		
		if ($request->isPost() && $request->has('firstname', 'post') && $this->validate()) {
			$sql = "insert into address set customer_id = '?', company = '?', firstname = '?', lastname = '?', address_1 = '?', address_2 = '?', postcode = '?', city = '?', zone_id = '?', country_id = '?'";
      		$database->query($database->parse($sql, $customer->getId(), $request->gethtml('company', 'post'), $request->gethtml('firstname', 'post'), $request->gethtml('lastname', 'post'), $request->gethtml('address_1', 'post'), $request->gethtml('address_2', 'post'), $request->gethtml('postcode', 'post'), $request->gethtml('city', 'post'), $request->gethtml('zone_id', 'post'), $request->gethtml('country_id', 'post')));

	  		$session->set('shipping_address_id', $database->getLastId());

	  		$response->redirect($url->ssl('checkout_shipping'));
    	}
	
		$template->set('content', $this->getForm());
	
		$template->set($module->fetch());
	
    	$response->set($template->fetch('layout.tpl'));
  	}
  
  	function payment() {
 		$cart     =& $this->locator->get('cart');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		   	
		$language->load('controller/checkout_address.php');

    	$template->set('title', $language->get('heading_title')); 
		
		if (!$customer->isLogged()) {  
      		$session->set('redirect', $url->ssl('checkout_shipping'));

      		$response->redirect($url->ssl('account_login'));
    	}
		 	 
    	if (($request->isPost()) && ($request->has('address_id', 'post'))) {
	  		$session->set('payment_address_id', $request->gethtml('address_id', 'post'));
	  		
			$response->redirect($url->ssl('checkout_payment'));
		} 
	   
		if ($request->isPost() && $request->has('firstname', 'post') && $this->validate()) {
  	  		$sql = "insert into address set customer_id = '?', company = '?', firstname = '?', lastname = '?', address_1 = '?', address_2 = '?', postcode = '?', city = '?', zone_id = '?', country_id = '?'";
      		$database->query($database->parse($sql, $customer->getId(), $request->gethtml('company', 'post'), $request->gethtml('firstname', 'post'), $request->gethtml('lastname', 'post'), $request->gethtml('address_1', 'post'), $request->gethtml('address_2', 'post'), $request->gethtml('postcode', 'post'), $request->gethtml('city', 'post'), $request->gethtml('zone_id', 'post'), $request->gethtml('country_id', 'post')));
	  
	  		$session->set('payment_address_id', $database->getLastId());
	    
	  		$response->redirect($url->ssl('checkout_payment'));
    	}
	
    	$template->set('content', $this->getForm());
	
		$template->set($module->fetch());
	
    	$response->set($template->fetch('layout.tpl'));    
  	}
  
  	function getForm() {
		$address  =& $this->locator->get('address');
		$config   =& $this->locator->get('config');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');
		$template =& $this->locator->get('template');
				
    	$view = $this->locator->create('template');

		$view->set('tpl', $template);

    	$view->set('heading_title', $language->get('heading_title'));

    	$view->set('text_new_address', $language->get('text_new_address'));
   	 	$view->set('text_entries', $language->get('text_entries'));

    	$view->set('entry_firstname', $language->get('entry_firstname'));
    	$view->set('entry_lastname', $language->get('entry_lastname'));
    	$view->set('entry_company', $language->get('entry_company'));
    	$view->set('entry_address_1', $language->get('entry_address_1'));
    	$view->set('entry_address_2', $language->get('entry_address_2'));
    	$view->set('entry_postcode', $language->get('entry_postcode'));
    	$view->set('entry_city', $language->get('entry_city'));
    	$view->set('entry_country', $language->get('entry_country'));
    	$view->set('entry_zone', $language->get('entry_zone'));
    
		$view->set('button_continue', $language->get('button_continue'));
    
		$view->set('error_firstname', @$this->error['firstname']);
    	$view->set('error_lastname', @$this->error['lastname']);
    	$view->set('error_address_1', @$this->error['address_1']);
    	$view->set('error_city', @$this->error['city']);

    	$view->set('action', $url->ssl('checkout_address', $request->gethtml('action')));
				
    	$view->set('default', $session->get($request->gethtml('action') . '_address_id'));

    	$address_data = array();

    	$results = $database->getRows("select *, c.name as country, z.name as zone from address a left join country c on a.country_id = c.country_id left join zone z on a.zone_id = z.zone_id where a.customer_id = '" . (int)$customer->getId(). "'");

    	foreach ($results as $result) {
      		$address_data[] = array(
        		'address_id' => $result['address_id'],
	    		'address'    => $result['firstname'] . ' ' . $result['lastname'] . ', ' . $result['address_1'] . ', ' . $result['city'] . ', ' . (($result['zone']) ? $result['zone']  . ', ' : FALSE) . $result['country'],
        		'href'       => $url->ssl('account_address', $request->gethtml('action'), array('address_id' => $result['address_id']))
      		);
    	}

    	$view->set('addresses', $address_data);

    	$view->set('firstname', $request->gethtml('firstname', 'post'));
    	$view->set('lastname', $request->gethtml('lastname', 'post'));
    	$view->set('company', $request->gethtml('company', 'post'));
    	$view->set('address_1', $request->gethtml('address_1', 'post'));
    	$view->set('address_2', $request->gethtml('address_2', 'post'));
    	$view->set('city', $request->gethtml('city', 'post'));
    	$view->set('postcode', $request->gethtml('postcode', 'post'));

    	if ($request->has('country_id', 'post')) {
      		$view->set('country_id', $request->gethtml('country_id', 'post'));
    	} else {
      		$view->set('country_id', $config->get('config_country_id'));
    	}
 
    	if ($request->has('zone_id', 'post')) {
      		$view->set('zone_id', $request->gethtml('zone_id', 'post'));
    	} else {
      		$view->set('zone_id', $config->get('config_zone_id'));
    	}

    	$view->set('countries', $database->cache('country', "select * from country where status = '1' order by name"));

    	$view->set('zones', $database->cache('zone', "select * from zone order by country_id, name"));

    	return $view->fetch('content/checkout_address.tpl'); 
  	}

  	function validate() {
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
			
    	if ((strlen($request->gethtml('firstname', 'post')) < 3) || (strlen($request->gethtml('firstname', 'post')) > 32)) {
      		$this->error['firstname'] = $language->get('error_firstname');
    	}

    	if ((strlen($request->gethtml('lastname', 'post')) < 3) || (strlen($request->gethtml('lastname', 'post')) > 32)) {
      		$this->error['lastname'] = $language->get('error_lastname');
    	}

    	if ((strlen($request->gethtml('address_1', 'post')) < 3) || (strlen($request->gethtml('address_1', 'post')) > 64)) {
      		$this->error['address_1'] = $language->get('error_address_1');
    	}

    	if ((strlen($request->gethtml('city', 'post')) < 3) || (strlen($request->gethtml('city', 'post')) > 32)) {
      		$this->error['city'] = $language->get('error_city');
    	} 
	
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}  
  	}

	function zone() {
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		 
    	$results = $database->cache('zone-' . (int)$request->gethtml('country_id'), "select zone_id, name from zone where country_id = '" . (int)$request->gethtml('country_id') . "' order by name");

    	$output = '<select name="zone_id">';
        
		foreach ($results as $result) {
        	$output .= '<option value="' . $result['zone_id'] . '"';
	
	    	if ($request->gethtml('zone_id') == $result['zone_id']) {
	      		$output .= ' SELECTED';
	    	}
	
	    	$output .= '>' . $result['name'] . '</option>';
      	}
	
		if (!$results) {
      		$output .= '<option value="0">' . $language->get('text_none') . '</option>';
    	}

    	$output .= '</select>';
	
		$response->set($output);
  	} 
}
?>
